package cn.tedu.net.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServerSocketDemo {
    public static void main(String[] args) throws IOException {
        //创建TCP服务器端对象
        ServerSocket ss=new ServerSocket(9090);

        //接收连接
        Socket s = ss.accept();//会线程对象阻塞


        //获取自带的字节输入流
        InputStream is = s.getInputStream();

        //读取数据
        //自建缓冲区
        byte[] bs=new byte[1024];
        int len=-1;
        while ((len=is.read(bs))!=-1){//会让线程对象阻塞
            System.out.println(new String(bs,0,len));
        }
        //通知客户端数据已经读取完毕
        s.shutdownInput();

        //关流
        s.close();
        ss.close();
    }
}
